package org.dx.oho.service.impl;

import org.dx.oho.dao.ShopDao;
import org.dx.oho.dto.ShopExecution;
import org.dx.oho.entity.Shop;
import org.dx.oho.enums.ShopStateEnum;
import org.dx.oho.exception.ShopOperationException;
import org.dx.oho.service.ShopService;
import dx.util.FileUtil;
import dx.util.PathUtil;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * 店铺service
 *@description： 1：新增一个店铺
 *@classname: ShopServiceImp
 *@author: duanx
 *@date: 2021/1/31 22:51
 **/
@Service
public class ShopServiceImpl implements ShopService {
    private final ShopDao shopDao;
    private Logger logger = LoggerFactory.getLogger(ShopServiceImpl.class);
    public ShopServiceImpl(ShopDao shopDao) {
        this.shopDao = shopDao;
    }

    @Override
    @Transactional(rollbackFor = ShopOperationException.class)
    public ShopExecution addShop(Shop shop, InputStream shopImgInputStream, String fileName) {
        if (shop == null){
            logger.error("店铺参数为空");
            return new ShopExecution(ShopStateEnum.NULL_SHOP);
        }
        try {
            //初始化信息
            logger.info("初始化店铺信息......");
            shop.setEnableStatus(0);
            shop.setCreateTime(new Date());
            shop.setLastEditTime(new Date());
            //添加店铺
            int effectNum = shopDao.insertShop(shop);
            if (effectNum <= 0) {
                logger.error("插入店铺失败！");
                throw new ShopOperationException("店铺创建失败");
            }else {
                if (shopImgInputStream !=null){
                    //存储图片
                    try {
                        addShopImg(shop, shopImgInputStream,fileName);
                    }catch (Exception e){
                        logger.error("添加图片失败");
                        throw new ShopOperationException("add shop img error"+e.toString());
                    }
                    effectNum = shopDao.updateShop(shop);
                    if (effectNum<=0){
                        throw new ShopOperationException("更新图片地址失败");
                    }
                }
            }

        }catch (Exception e){
            logger.error("插入店铺失败"+e.toString());
            throw new ShopOperationException("add shop error:"+e.toString());
        }
        logger.info("添加\""+shop.getShopName()+"\"店铺成功");
        return new ShopExecution(ShopStateEnum.CHECK,shop);
    }

    @Override
    public List<Shop> getShop(Shop shop) {
        return shopDao.selectShop(shop);
    }

    /**
     * 添加图片，并将相对路径更新到店铺对象上
     * @param shop 店铺对象
     * @param shopImgInputStream 图片对象
     * @param fileName 图片名称。
     */
    private void addShopImg(@NotNull Shop shop, InputStream shopImgInputStream,String fileName){
        //使用店铺id作为文件夹名称
        String dest = PathUtil.getShopImagePath(shop.getShopId());
        //创建缩略图文件
        String shopImgAddr = FileUtil.generateThumbnail(shopImgInputStream,dest,fileName);
        String suffix = fileName.substring(fileName.lastIndexOf("."));
        //更新路径
        shop.setShopImg(dest+shopImgAddr+suffix);
        logger.info("生成店铺“"+shop.getShopName()+"”缩略图成功，路径："+dest+shopImgAddr+suffix);
    }
}
